// Copyright (c) 2008, 
// Marc Speck, http://www.faindu.com
// All Rights Reserved. The following is Source Code and is subject to all restrictions 
// on such code as contained in the license accompanying this product.
package com.faindu.editImage.move
{
	import com.faindu.editImage.EditImage;
	import com.faindu.editImage.base.BaseTool;
	import com.faindu.editImage.base.EditImageEvent;
	import com.faindu.editImage.base.IEditImageTools;
	
	import flash.geom.Matrix;
	import flash.geom.Point;
	
/**
 * Tool that moves the image on the display. By default, 
 * <code>MoveTool</code> is disabled.
 */
public class MoveTool extends BaseTool implements IMoveTool
{
    //-------------------------------------------------------------------------- 
    //
    //  Variables
    //
    //--------------------------------------------------------------------------  	
	//----------------------------------
	//  interactionStartMatrix
	//----------------------------------
	/**
	 * The <code>toolMatrix</code> at the start of the interaction
	 */
	private var interactionStartMatrix: Matrix

	//----------------------------------
	//  interactionStart
	//----------------------------------
	/**
	 * Set to <code>mouseLoc.clone()</code> in startInteractionHandler 
	 * 
	 * @see #startInteractionHandler()
	 */
 	private var interactionStart:Point = new Point() 

	//----------------------------------
	//  movingAreaWidth
	//----------------------------------
	protected var _movingAreaWidth: Number = 500;
	/**
	 * @inheritDoc
	 * 
	 * @default 500
	 */
	public function get movingAreaWidth(): Number
	{
		return _movingAreaWidth
	}
	public function set movingAreaWidth( value:Number ): void
	{
		_movingAreaWidth = value
	}

	//----------------------------------
	//  movingAreaHeight
	//----------------------------------	
	protected var _movingAreaHeight: Number = 500;
	/**
	 * @inheritDoc
	 * 
	 * @default 500
	 */
	public function get movingAreaHeight(): Number
	{
		return _movingAreaHeight
	}
	public function set movingAreaHeight( value:Number ): void
	{
		_movingAreaHeight = value
	}
	
    //-------------------------------------------------------------------------- 
    //
    //  Properties
    //
    //--------------------------------------------------------------------------  	
	/**
	 * Even though no control is visible to the user, <code>moveControl</code> is
	 * required for e.g. handling the cursor.
	 */
	public var moveControl: MoveControl

    //----------------------------------
	//  Enabled
	//----------------------------------		
	/**
	 * @inheritDoc
	 * 
	 * @default false
	 * 
	 * @internal See also #visible 
	 */
	override public function set enabled( value:Boolean):void 
	{
		if (_enabled != value) 
		{
			_enabled = value
			if ( editImageTools )
			{
				moveControl.enabled = _enabled
				editImageTools.updateToolsEnabled()
			}
		}
	}
	
    //-------------------------------------------------------------------------- 
    //
    //  Methods
    //
    //--------------------------------------------------------------------------  			
	/**
	 * @inheritDoc
	 */
	override public function init( editImage:EditImage, startInteractionHandler:Function ): void
	{
		// initialize properties
		editImageTools = IEditImageTools( editImage )
		editImage.addEventListener( EditImageEvent.CONTROL_DOWN, startControlInteraction )
				
		// create all controls
		moveControl = new MoveControl( moveInteraction )
		moveControl.init( editImage, startInteractionHandler, this )

		// set the inital value of the area in which the image is allowed to move.
		movingAreaWidth = editImage.movingAreaWidth
		movingAreaHeight = editImage.movingAreaHeight
	}

	//==========================================================================
    //
    //  Move interaction
    //
    //--------------------------------------------------------------------------  	
	/**
	 * @inheritDoc
	 */
	override protected function startControlInteraction( editImageEvent:EditImageEvent ): void
	{
		if ( enabled )
		{
			interactionStartMatrix =  editImageTools.toolMatrix.clone()
			interactionStart = editImageEvent.mousePosition
		}
	}
	    
	/**
	 * Moves the image around.
	 * 
	 * @param editImageEvent is thrown by interactionHandler.
	 * 
	 * @see com.faindu.editImage.EditImage.interactionHandler() 
	 */
	public function moveInteraction( editImageEvent:EditImageEvent ):void 
	{
		var moveLoc:Point = editImageEvent.mousePosition.subtract( interactionStart )
		var movedMatrix: Matrix = interactionStartMatrix.clone()

		movedMatrix.tx += moveLoc.x
		movedMatrix.ty += moveLoc.y
		
		editImageTools.toolMatrix = movedMatrix
	}
}
}